#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->lineEdit->setPlaceholderText("请输入新增的列名");
    //新增三行
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);

    //新增三列
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);

    ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("年龄"));
    ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("住址"));

    ui->tableWidget->setItem(0, 0, new QTableWidgetItem("张三"));
    ui->tableWidget->setItem(0, 1, new QTableWidgetItem("19"));
    ui->tableWidget->setItem(0, 2, new QTableWidgetItem("西安"));

    ui->tableWidget->setItem(1, 0, new QTableWidgetItem("李四"));
    ui->tableWidget->setItem(1, 1, new QTableWidgetItem("20"));
    ui->tableWidget->setItem(1, 2, new QTableWidgetItem("杭州"));

    ui->tableWidget->setItem(2, 0, new QTableWidgetItem("王五"));
    ui->tableWidget->setItem(2, 1, new QTableWidgetItem("21"));
    ui->tableWidget->setItem(2, 2, new QTableWidgetItem("北京"));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    //获取当前行号
    int row = ui->tableWidget->rowCount();
    //插入新的行
    ui->tableWidget->insertRow(row);
}


void Widget::on_pushButton_2_clicked()
{
    const QString& text = ui->lineEdit->text();
    //有文本则新增一行
    if(text.isEmpty())
    {
        return;
    }
    else {
            //获取当前的列号
            int column = ui->tableWidget->columnCount();
            ui->tableWidget->insertColumn(column);
            ui->tableWidget->setHorizontalHeaderItem(column, new QTableWidgetItem(text));
    }

}


void Widget::on_pushButton_3_clicked()
{
    int row = ui->tableWidget->currentRow();

    ui->tableWidget->removeRow(row);
}


void Widget::on_pushButton_4_clicked()
{
    int column = ui->tableWidget->currentColumn();

    ui->tableWidget->removeColumn(column);
}

